package com.gshocklab.bulkposter.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gshocklab.bulkposter.AppMainActivity;
import com.gshocklab.bulkposter.R;
import com.gshocklab.bulkposter.data.AccountType;
import com.gshocklab.bulkposter.data.AccountTypesProvider;
import com.gshocklab.bulkposter.view.helper.ApplicationDialogs;
import com.gshocklab.bulkposter.view.helper.TwitterCharsCounter;
import com.gshocklab.bulkposter.view.helper.ApplicationDialogs.IConfirmDialogListener;

/**
 * Activity for message (tweet, status update, post etc.) screen
 * @author vitaly gashock
 */

public class ComposerActivity extends Activity {
	private int accountTypeId = 0;
	
	private ImageView accountIcon;
	private TextView accountPromptTitle;
	private TextView twitterCharsCounter;
	private EditText postMessageEdit;
	private Button sendButton;
	private Button cancelButton;
	
	private static final int MAX_TWITTER_POST_LENGTH = 140;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.composer);
		
		this.accountTypeId = getIntent().getIntExtra(AppMainActivity.ACCOUNT_TYPE_KEY, 0);
		initComposerView();
	}
	
	private void initComposerView() {
		// bind widgets from layout
		accountIcon = (ImageView) findViewById(R.id.account_type_icon);
		accountPromptTitle = (TextView) findViewById(R.id.account_type_title);
		twitterCharsCounter = (TextView) findViewById(R.id.twitter_counter);
		postMessageEdit = (EditText) findViewById(R.id.message_text);
		sendButton = (Button) findViewById(R.id.send_button);
		cancelButton = (Button) findViewById(R.id.cancel_button);
	
		initPostViewIcon();
		initPostViewTitle();
		initTwitterCharsCounterView();
		initViewButtons();
		initMessageTextHandler();
	}
	
	private void initPostViewIcon() {
		accountIcon.setImageResource(AccountTypesProvider.accountTypes.get(accountTypeId).bigIconId);
	}
	
	private void initPostViewTitle() {
		String viewTitle = "";
		
		switch (accountTypeId) {
			case AccountType.BUZZ_ACCOUNT :
				viewTitle = "Make new post";
				break;
			case AccountType.FACEBOOK_ACCOUNT :
				viewTitle = "What\'s in your mind?";
				break;
			case AccountType.LINKEDIN_ACCOUNT :
				viewTitle = "New status update";
				break;
			case AccountType.TWITTER_ACCOUNT :
				viewTitle = "Create new tweet";
				break;
			case AccountType.VKONTAKTE_ACOUNT :
				viewTitle = "New network status";
				break;
		}
		
		accountPromptTitle.setText(viewTitle);
	}
	
	private void initTwitterCharsCounterView() {
		twitterCharsCounter.setText(String.valueOf(MAX_TWITTER_POST_LENGTH));
		twitterCharsCounter.setVisibility(accountTypeId == AccountType.TWITTER_ACCOUNT ? View.VISIBLE : View.INVISIBLE);
	}
	
	private void initMessageTextHandler() {
		if (accountTypeId != AccountType.TWITTER_ACCOUNT) return;
		
		twitterCharsCounter.setTextColor(Color.DKGRAY);
		postMessageEdit.addTextChangedListener(new TwitterCharsCounter() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				int enteredLength = s.length();
				int counter = MAX_TWITTER_POST_LENGTH - enteredLength;
				
				twitterCharsCounter.setTextColor(counter <= 0 ? Color.RED : Color.DKGRAY);
				twitterCharsCounter.setText(String.valueOf(counter));
			}
		});
	}
	
	private void initViewButtons() {
		sendButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String postMessage = postMessageEdit.getText().toString();
				
				if (accountTypeId == AccountType.TWITTER_ACCOUNT) {
					// crop message length if it really needed
					if (postMessage.length() > MAX_TWITTER_POST_LENGTH) {
						postMessage = postMessage.substring(0, MAX_TWITTER_POST_LENGTH - 1);
					}
				}
				
				final Intent resultIntent = new Intent();
				resultIntent.putExtra(AppMainActivity.POST_MESSAGE_KEY, postMessage);
				setResult(RESULT_OK, resultIntent);
				finish();
			}
		});
		
		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// check that user doesn't have entered text
				if (postMessageEdit.getText().length() > 0) {
					ApplicationDialogs.showConfirmDialog(ComposerActivity.this, 
							"Confirm", "Proceed closing composer without saving post?", new IConfirmDialogListener() {
								@Override
								public void onYes() {
									setResult(RESULT_CANCELED);
									finish();
								}
								
								@Override
								public void onNo() { }
							});
				} else {
					setResult(RESULT_CANCELED);
					finish();
				}
				
			}
		});
	}
}
